//
//  JAHomeViewController.m
//  网易新闻
//
//  Created by Arvin on 17/3/18.
//  Copyright © 2017年 Arvin. All rights reserved.
//

#import "JAHomeViewController.h"
#import "JAChannelView.h"
#import <Masonry.h>
#import "JAListViewController.h"

@interface JAHomeViewController ()<UIPageViewControllerDataSource,JAChannelViewDelegate,UIPageViewControllerDelegate>{
    JAChannelView *channelView;
    UIPageViewController *pageViewController;
}

@end

@implementation JAHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addChannelView];
    
    [self setPageViewController];
}


//添加频道视图
-(void)addChannelView{
    
    channelView = [[JAChannelView alloc]init];
    
    //设置代理
    channelView.channelDelegate = self;
    
    [self.view addSubview:channelView];
    
    [channelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(40);
        make.top.equalTo(self.view).offset(64);
    }];
    
   
}

//实现代理方法
//切换频道代理
-(void)channelChanged:(NSInteger)index{
    
    NSLog(@"切换到频道: %ld", index);
    
    //获取到当前的子控制器的index
    JAListViewController *newList = (JAListViewController *)pageViewController.viewControllers[0];
    NSInteger currentIndex = newList.index;
    
    //如果当前的index和被点击的index值一样,则什么都不需要做
    if(currentIndex == index) return;
   
    //如果点击的index较大,从右往左边推,如果较小,从左往右边推
    NSInteger direction = currentIndex > index ? UIPageViewControllerNavigationDirectionReverse :UIPageViewControllerNavigationDirectionForward;
    
    //创建将要显示的子控制器
    JAListViewController *controller = [[JAListViewController alloc]initWithIndex:index channelModel:channelView.channelModelArr[index]];
    NSArray *controllers  = @[controller];
    
    //重新设置pageViewController的子控制器,每次调用 setViewControllers 的方法都会将子控制器重新推一次

    [pageViewController setViewControllers:controllers direction:direction animated:YES completion:nil];
}

//设置左右滑的新闻列表页的pageViewController
-(void)setPageViewController{
    //创建pageViewController
    pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey:@0}];

     //2. 将pageViewController添加到RootViewController
    [self addChildViewController:pageViewController];
    [self.view addSubview:pageViewController.view];
    [pageViewController didMoveToParentViewController:self];
    
    //调整pageViewController的布局
    [pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(channelView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];
    
    //3.添加子控制器
    JAListViewController *controller = [[JAListViewController alloc]initWithIndex:0 channelModel:channelView.channelModelArr[0]];
    NSArray *controllers = @[controller];
    [pageViewController setViewControllers:controllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    //4.实现数据源
    pageViewController.dataSource = self;
    
    //设置代理
    pageViewController.delegate = self;
}

#pragma mark - pageViewController的数据源方法
//生成前一个控制器
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(JAListViewController *)viewController{
    
    //滚到头了
    if(viewController.index == 0)
    {
        return nil;
    }
    
    NSInteger index = viewController.index - 1;
    
    JAListViewController *controller = [[JAListViewController alloc]initWithIndex:index channelModel:channelView.channelModelArr[index]];
    
    return controller;
}

//生成后一个控制器
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(JAListViewController *)viewController{
    
    //滚到尾了
    if(viewController.index == channelView.channelModelArr.count - 1)
    {
        return nil;
    }
    NSInteger index = viewController.index + 1;
    JAListViewController *controller = [[JAListViewController alloc]initWithIndex:index channelModel:channelView.channelModelArr[index]];
    
    return controller;
}

//pageViewController的子控件滑动结束的时候调用
-(void)pageViewController:(UIPageViewController *)_pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(nonnull NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    
    //获得当前子控制器的index
    JAListViewController *newList = (JAListViewController *)_pageViewController.viewControllers[0];
    channelView.selectedIndex = newList.index;
}
@end
